package edu.protbioinfo;

import java.io.IOException;
import java.io.PrintStream;



public class Matrix<T> {
	protected int rows;
	protected int cols;
	
	protected T[] data;
	
	public Matrix(int rows, int cols){
		if(rows <= 0 || cols <= 0){
			throw new IllegalArgumentException("rows or cols should be greater than 0");
		}
		
		this.rows = rows;
		this.cols = cols;
		
		this.data = (T[]) new Object[rows * cols];
	}
	
	public void setValue(int i, int j, T value){
		data[getIndex(i, j)] = value;
	}
	

	public T getValue(int i, int j){
		return data[getIndex(i, j)];
	}
	
	public int getRows(){
		return rows;
	}
	
	public int getCols(){
		return cols;
	}
	
	public T[] getData() {
		return data;
	}

	public void setData(T[] data) {
		this.data = data;
	}

	public String toPrettyString(){
		StringBuffer sb = new StringBuffer();
		String newLine = System.getProperty("line.separator");

		for(int i = 0; i < rows; i++){
			for(int j = 0; j < cols; j++){
				sb.append(getValue(i, j) + "\t");
			}
			sb.append(newLine);
			sb.append(newLine);
		}
		
		return sb.toString();
	}

	protected int getIndex(int i, int j) {
		int index = i * cols + j;
		if(index < 0 || index >= data.length){
			throw new IllegalArgumentException("Illegal index (" + i + ", " + j + ")");
		}
		return index;
	}
	

}
